package com.netty;

import com.sun.corba.se.internal.CosNaming.BootstrapServer;
import com.sun.webkit.EventLoop;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import javax.swing.event.EventListenerList;
import java.nio.channels.ServerSocketChannel;

public class NettyServer {
    /**
     * 体验代码，此处使用4.1.69版本netty
     * 3.x:比较老的版本
     * 4.x:当前稳定版本
     * 4.1.x:兼容4.0老版本，有些新特性http://ifeve.com/new-and-noteworthy-in-4-1/
     * 5.x:废弃版本，废弃原因https://github.com/netty/netty/issues/4466
     * @param args
     */



    public static void main(String[] args) {
        try {
            new NettyServer().open();
        } catch (Exception e) {

        }

    }

    public void open() throws Exception {
        // 创建bossgroup连接处理channel线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        // 创建worketGroup数据业务时间处理线程组
        EventLoopGroup workGroup = new NioEventLoopGroup(16);

        // 创建启动对象
        ServerBootstrap serverBootstrap = new ServerBootstrap();

        serverBootstrap.group(bossGroup,workGroup)// 设置线程池
                .channel(NioServerSocketChannel.class)// 通道实现(作用暂不知)
                .option(ChannelOption.SO_BACKLOG,1024)//
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        // sockerChannel处理器
                        socketChannel.pipeline().addLast(new NettyServerHandler());
                    }
                });

        System.out.println("netty start");

        // 绑定端口
        ChannelFuture channelFuture = serverBootstrap.bind(18801).sync();








    }

}
